﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class RoleDAO : SqlDAO
    {
        public const string SP_GET_LIST_PAGING_ROLE = "spGetListPagingRole";
        public const string SP_GET_ALL_ROLE = "spGetAllRole";
        public const string SP_GET_ALL_ROLE_BY_USERID_APPCODE = "spGetAllRoleByUserIdAndAppCode";
        public const string SP_GET_LIST_PAGING_ROLE_BY_ID = "spGetRoleById";
        public const string SP_GET_ALL_ROLE_BY_APPID = "spGetAllRoleByAppId";
        public const string SP_CREATE_OR_UPDATE_ROLE = "spCreateOrUpdateRole";
        public const string SP_DELETE_ROLE = "spDeleteRole";
        public const string SP_ROLE_NAME_BY_USER_ID = "spGetRoleNameByUserId";
        public const string SP_ROLE_NAME_BY_GROUP_ID = "spGetRoleNameByGroupId";            
        public const string SP_GET_ROLE_LIST_BY_APP_ID = "spGetRoleListByAppId";
        
        public const string Param_RoleID = "@RoleId";
        public const string Param_RoleName = "@RoleName";
        public const string Param_RoleCode = "@RoleCode";
        public const string Param_UserId = "@UserId";
        public const string Param_GroupId = "@GroupId";
        public const string Param_AppCode = "@AppCode";
        public const string Param_AppId = "@AppID";
        public const string param_Descriptsions = "@RoleDescription";

        public RoleDAO(String connectionString, System.Type type = null)
            : base(connectionString, type) { ;}

        internal DataPage<RoleModel> GetDynamicListPaging(RoleModel model, int pageSize, int pageIndex, Dictionary<String, String> mapDictionary, bool compareEqual)
        {
            try
            {
                //SqlCommand command = SharedSqlDao.GetSprocCommand(pr_Person_GetAll);
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_ROLE);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_RoleCode, compareEqual ? model.RoleCode : ("%" + model.RoleCode + "%")));
                    command.Parameters.Add(CreateParameter(Param_RoleName, compareEqual ? model.RoleName : ("%" + model.RoleName + "%")));
                    command.Parameters.Add(CreateParameter(Param_AppId, model.AppId));
                }
                command.Parameters.Add(CreateParameter("@PageIndex", pageIndex));
                command.Parameters.Add(CreateParameter("@PageSize", pageSize));
                return GetDataPage<RoleModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal RoleModel GetById(int? ID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_ROLE_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_RoleID, ID));

                return GetSingle<RoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateRole(RoleModel model)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_ROLE);
                command.Parameters.Add(new SqlParameter(Param_RoleID, model.RoleId));
                command.Parameters.Add(CreateParameter(Param_AppId, model.AppId));
                command.Parameters.Add(CreateParameter(Param_RoleName, model.RoleName));
                command.Parameters.Add(CreateParameter(Param_RoleCode, model.RoleCode));
                command.Parameters.Add(CreateParameter(param_Descriptsions, model.RoleDescription));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteRole(int[] ID)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_ROLE);

                command.Parameters.Add(new SqlParameter("@RoleList", GetTableId<int>(ID, "RoleID")));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }
        internal List<RoleModel> GetAll(Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ALL_ROLE);
                return GetList<RoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal IEnumerable<RoleModel> GetAllByUserIdAndAppCode(int userId, string appCode, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ALL_ROLE_BY_USERID_APPCODE);
                command.Parameters.Add(CreateParameter(Param_UserId, userId));
                command.Parameters.Add(CreateParameter(Param_AppCode, appCode));
                return GetList<RoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }
        internal IEnumerable<RoleModel> GetAllByAppId(int appid, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ALL_ROLE_BY_APPID);
                command.Parameters.Add(CreateParameter(Param_AppId, appid));
                return GetList<RoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<RoleModel> GetRoleNameByUserId(int userId,Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_ROLE_NAME_BY_USER_ID);
                command.Parameters.Add(CreateParameter(Param_UserId, userId));
                return GetList<RoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<RoleModel> getRoleNameByGroupId(int? groupId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_ROLE_NAME_BY_GROUP_ID);
                command.Parameters.Add(CreateParameter(Param_GroupId, groupId));
                return GetList<RoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<RoleModel> GetListRoleByApp(int AppID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_ROLE_LIST_BY_APP_ID);
                command.Parameters.Add(CreateParameter(Param_AppId, AppID));
                return GetList<RoleModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }
    }
}
